<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="storeName" class="w-72 ml-2">
          <a-input v-model:value="formState.storeName" placeholder="药房名称/药房编号" allow-clear />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3">
        <BaseButton type="primary" class="mb-3 mr-2" @click="onClickAdd()">新增</BaseButton>
        <BaseExport type="运维" name="药房维护" code="00001132" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable :loading="loading" column-code="00001132" :data-source="tableData" :pagination="pagination" :scroll="{ x: 1800 }" fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'storeTypeStr'"> {{ record.storeTypeStr ? record.storeTypeStr : '-' }} </template>
          <template v-if="column.dataIndex === 'statusStr'">
            <a-switch v-model:checked="record.status" checked-value="1" un-checked-value="0" @change="onChangeStatus(record, $event)" />
          </template>
          <template v-if="column.dataIndex === 'isProducer'">
            <a-switch v-model:checked="record.isProducer" checked-value="1" un-checked-value="0" @change="onChangeFaStatus(record, $event)" />
          </template>
          <template v-if="column.dataIndex === 'isLocalStoreStr'">
            <a-tag :color="record.isLocalStore === 0 ? 'green' : 'blue'">{{ record.isLocalStore === 0 ? '是' : '否' }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'ckmc'">
            {{ record.isLocalStore === 0 ? record.ckmc : '-' }}
          </template>

          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickEdit(record)">修改</a-button>
            <a-button type="link" size="small" @click="onClickProtect(record)">维护人员</a-button>
            <BaseButton confirm-text="确认删除？" type="link" danger size="small" @click="onDelete(record)">删除</BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" />
    <ProtectModal v-model:open="protectOpen" :row-data="rowData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import Edit from './components/Edit'
import ProtectModal from './components/ProtectModal'

const formState = ref({ processMethodName: '' })
const formRef = ref(null)
const editOpen = ref(false)
const protectOpen = ref(false)
const rowData = ref(null)
const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/opex/getDrugStoreInfoList',
  immediate: true,
  params: formState.value
})
//启用
const onChangeStatus = async (record, number) => {
  const res = await post(number === '1' ? '/operation/opex/doEnableDrugStore' : '/operation/opex/doDisableDrugStore', {
    id: record.id
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
//是否发药药房
const onChangeFaStatus = async (record, number) => {
  const res = await post('/operation/opex/doDisableDrugStoreProducer', {
    id: record.id,
    isProducer: number
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
const onDelete = async (record) => {
  const res = await post('/operation/opex/doDelDrugStoreInfo', {
    id: record.id
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
const onClickEdit = (record = {}) => {
  editOpen.value = true
  rowData.value = record
}
// 维护人员
const onClickProtect = (row) => {
  rowData.value = { ...row }
  protectOpen.value = true
}
const onSuceess = () => {
  editOpen.value = false
  getData()
}
const onClickAdd = () => {
  rowData.value = null
  editOpen.value = true
}
</script>

<style lang="less" scoped></style>
